/*
 * Debts Manager. Simple android application for managing debts.
 * Copyright (C) 2010 Maxim Manuylov
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/gpl-2.0.html>.
 */

package manuylov.maxim.dm.data.object;

import manuylov.maxim.dm.Constants;

import java.util.Set;

public class DebtsSummary implements DebtsListItem {
    private final Subject mySubject;
    private final int myBalance;

    public DebtsSummary(final Subject subject, final Set<Debt> debts) {
        mySubject = subject;
        int balance = 0;
        for (final Debt debt : debts) {
            balance += debt.getRemainder() * (2 * debt.getRecordType() - 1);
        }
        myBalance = balance;
    }

    public int getId() {
        return -1;
    }

    public int getRecordType() {
        return myBalance == 0 ? Constants.NONE : (myBalance > 0 ? Constants.LEND : Constants.BORROW);
    }

    public boolean isSummaryItem() {
        return true;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder(myBalance > 0 ? "+" : "");
        sb.append(myBalance);
        sb.append(" ");
        sb.append(mySubject);
        return sb.toString();
    }
}
